// *** COLLISION OBJECT *** //
// Controls the collision system for the game
//
// @project: GoldenShield
// @author: Kasper B. Jensen(kapper14@gmail.com)
// @version: 1.0
    
    var collision = {
        player: {},                                                                                                     //Holds the player object
        playerPoint: {topLeft: {x: 0, y: 0}, topRight: {x: 0, y: 0}, botLeft: {x: 0, y: 0}, botRight: {x: 0, y: 0}},    //Holds the different player collision points
        showPlayerpoints: false,                                                                                        //Tells if collision points should be showed visually
        divsAppended: false,                                                                                            //Tells if the visual collision points already is appended
        collisionType: {top: false, left: false, bottom: false, right: false},                                          //Tells where the collision is comming from
        collisionClass: "colobject",                                                                                    //Tells which divs that should be collsion detected.
        collisionObject: null,                                                                                          //Holds the object that is being collided with
        collidedObjects: 0,                                                                                             //Tells how many objects that the player is colliding with.
        
        //Resets the collision obejct back to default values!
        resetCollision: function() {
            this.player = {};
            this.playerPoint = {topLeft: {x: 0, y: 0}, topRight: {x: 0, y: 0}, botLeft: {x: 0, y: 0}, botRight: {x: 0, y: 0}};
            this.showPlayerpoints = false;
            this.divsAppended = false;
            this.collisionType = {top: false, left: false, bottom: false, right: false};
            this.collisionClass =  "colobject";
            this.collisionObject = null;
            this.collidedObjects = 0;
        },
                
        //Detects collision from player position
        detect: function(player) {
            if(player) { //If player is given
            
                //Setting the player object
                this.player = player;
            
                //Setting playerpoints information
                this.setPlayerPoints();
                
                //Chekking to see if there is a collision
                this.updatePlayerPoints();
                this.isCollision();
                    
                if(!this.isCollision()) { //If there is no collision, reset the collision type.
                    this.collisionType = {top: false, left: false, bottom: false, right: false}
                }
                    
                //Returns an object with collision information
                return {collisionObject: this.collisionObject, collisionType: this.collisionType, detectedCollision: true}

            } else {
                //Gives an alert, if player object not is given in detect
                alert('No player object given in Collicion object!!!!');
                return {detectedCollision: false}
            }
        },
        
        //Setting the different cillision points on player
        setPlayerPoints: function() {
            
            //Setting point 1 in topLeft corner
            this.playerPoint.topLeft.x = this.player.x;
            this.playerPoint.topLeft.y = this.player.y + 20;
            
            //Setting point 2 in topRight corner
            this.playerPoint.topRight.x = this.player.x + this.player.width;
            this.playerPoint.topRight.y = this.player.y + 20;
            
            //Setting point 3 in botLeft corner
            this.playerPoint.botLeft.x = this.player.x;
            this.playerPoint.botLeft.y = this.player.y + this.player.height;
            
            //Setting point 4 in botRight corner
            this.playerPoint.botRight.x = this.player.x + this.player.width;
            this.playerPoint.botRight.y = this.player.y + this.player.height;
            
        },
        
        //Shows visually the collision points when called
        showPlayerPoints: function() {
            this.showPlayerpoints = true;
            $('.' + this.collisionClass).css({border: "1px solid red"});
        },
        
        //Updates the visually playerpoints on the player & shows a dubugpanel
        updatePlayerPoints: function() {
            if(this.showPlayerpoints) {
                
                //If not divs is appended, then append the collisionpoint divs
                if(!this.divsAppended) {
                    $('#scene').append('<div id="collisionPoint1" class="collisionPoint"></div><div id="collisionPoint2" class="collisionPoint"></div><div id="collisionPoint3" class="collisionPoint"></div><div id="collisionPoint4" class="collisionPoint"></div>')
                    $('body').append('<div id="collisionInfo"><p id="collisionInfoAlert"></p><p id="collisionInfoTop"></p><p id="collisionInfoLeft"></p><p id="collisionInfoBot"></p><p id="collisionInfoRight"></p><p id="playerX"></p><p id="playerY"></p></div>');
                    $('.collisionPoint').css({display: "block", position: "absolute", width: "5px", height: "5px", zIndex: "100", backgroundColor: "black"});
                    $('#collisionInfo').css({left: "0px", top: "0px", position: "absoulete", color: "white"})
                    this.divsAppended = true;
                }
                
                //Updates the CSS information for the playerPoints
                $('#collisionPoint1').css({left: this.playerPoint.topLeft.x + 'px' ,top: this.playerPoint.topLeft.y + 'px'});
                $('#collisionPoint2').css({left: this.playerPoint.topRight.x + 'px' ,top: this.playerPoint.topRight.y + 'px'});
                $('#collisionPoint3').css({left:  this.playerPoint.botLeft.x + 'px' ,top:  this.playerPoint.botLeft.y + 'px'});
                $('#collisionPoint4').css({left:  this.playerPoint.botRight.x + 'px' ,top:  this.playerPoint.botRight.y + 'px'}); 
                
                //Updates a dubug panel at left with collision information
                $('#collisionInfoTop').html('Top: ' + this.collisionType.top);
                $('#collisionInfoBot').html('Bot: ' + this.collisionType.bottom);
                $('#collisionInfoLeft').html('Left: ' + this.collisionType.left);
                $('#collisionInfoRight').html('Right: ' + this.collisionType.right);
                $('#playerX').html('Spillerposition X: ' + this.player.x);
                $('#playerY').html('Spillerposition Y: ' + this.player.y);
                
            }
        },
        
        //Detecting player collision with other object and return true/false on collsiion
        isCollision: function() {
            
            //Initializing variables
            var playerPoint = this.playerPoint;
            var collisionType = this.collisionType;
            var collisionAlert = false;
            var collsionObject = null;
            var top = false;
            var bot = false;
            var left = false;
            var right = false;
            
            $('.' + this.collisionClass).each(function() { //Foreach div that has the class 'colobject'
                
                    //Collision object information
                    var objectWith = parseInt($(this).css("width"));
                    var objectHeight = parseInt($(this).css("height"));
                    var objectPosX = parseInt($(this).css("left"));
                    var objectPosY = parseInt($(this).css("top"));
            
                    //Collision object points
                    var objectPointTopLeft = {x: objectPosX, y: objectPosY};
                    var objectPointTopRight = {x: objectPosX + objectWith, y: objectPosY}
                    var objectPointBotLeft = {x: objectPosX, y: objectPosY + objectHeight}
                    var objectPointBotRight = {x: objectPosX + objectWith, y: objectPosY + objectHeight};
                    
                    
                    //If player is colliding with an object
                    if(playerPoint.topRight.x >= objectPointTopLeft.x && playerPoint.topLeft.x <= objectPointTopRight.x && playerPoint.botRight.y >= objectPointTopRight.y && playerPoint.topRight.y <= objectPointBotRight.y) {
                            collisionAlert = true; //There is a collision
                            collsionObject = this; //This is the collision object
                            var playerPosX = player.x;
                            var playerPosY = player.y;
                            
                            if($(this).hasClass('stopPlayer')) {
                            //Player is colliding with the top of an object
                            if(playerPoint.botRight.y >= objectPointTopRight.y && playerPoint.botRight.y < objectPointBotRight.y && playerPoint.botLeft.y >= objectPointTopLeft.y && playerPoint.botLeft.y < objectPointBotLeft.y && playerPoint.topLeft.y < objectPointTopLeft.y) { //TOP
                                        top = true;
                                        playerPosY -= 2;
                                        if($(this).hasClass('shoot') != true) {
                                            player.move(playerPosX, playerPosY);
                                        }
                            }
                                
                            //Player is colliding with the bottom of an object
                            if(playerPoint.topRight.y <= objectPointBotRight.y && playerPoint.topRight.y > objectPointTopRight.y && playerPoint.topLeft.y <= objectPointBotLeft.y && playerPoint.topLeft.y > objectPointTopLeft.y && playerPoint.botLeft.y > objectPointBotLeft.y) { //BOT
                                        bot = true;
                                        playerPosY += 2;
                                        if($(this).hasClass('shoot') != true) {
                                            player.move(playerPosX, playerPosY);
                                        }
                            }
                            
                            //Player is colliding with the right of an object
                           if(playerPoint.topRight.x >= objectPointTopLeft.x && playerPoint.topRight.x < objectPointTopRight.x && playerPoint.botRight.x >= objectPointBotLeft.x && playerPoint.botRight.x < objectPointBotRight.x && playerPoint.topLeft.x < objectPointTopLeft.x) { //RIGHT
                                        right = true;
                                        playerPosX -= 2;
                                        if($(this).hasClass('shoot') != true) {
                                            player.move(playerPosX, playerPosY);
                                        }
                            }
                            
                            //Player is colliding with the left of an object
                           if(playerPoint.topLeft.x <= objectPointTopRight.x && playerPoint.topLeft.x > objectPointTopLeft.x && playerPoint.botLeft.x <= objectPointBotRight.x && playerPoint.botLeft.x > objectPointBotLeft.x && playerPoint.topRight.x > objectPointTopRight.x) { //LEFT
                                        left = true;
                                        playerPosX += 2;
                                        if($(this).hasClass('shoot') != true) {
                                            player.move(playerPosX, playerPosY);
                                        }
                            }                             
                            } 
                            collision.trigger(this); //Trigger the objects trigger functions from XMAP 
                            collision.checkSound(this);
                            collisionType = {top: top, left: left, bottom: bot, right: right} //Updates the collsion type
                            collision.collidedObjects++; //Adds 1 to the collision object counter
                            
                        }
            })
            
            //If the debugpanel is shown, then updates the alert information
            $('#collisionInfoAlert').html("Alert: " + collisionAlert + "<br/>CollisionObjects: " + this.collidedObjects);
            
            //If there is no more collsion alert, then reset collision type and remove hasTriggered class(see trigger function)
            if(!collisionAlert) {
                 collisionType = {top: false, left: false, bottom: false, right: false}
                 $('.hasTriggered').removeClass('hasTriggered');  
                 $('.surfaceSoundSet').removeClass('surfaceSoundSet');
                 $('.playerCollision').removeClass('playerCollision');
                 player.surfaceSound = null;
            }
            
            this.collidedObjects = 0; //Sets the colllsion object counter back to 0
                                      
            this.collisionType = collisionType; //Sets the collsion type
            this.collisionObject = collsionObject; //Sets the collision object
            
            //Return true/false on collision
            return collisionAlert;
        },
        
        //Changes the collision class
        setCollisionClass: function(className) {
            if(className.isString()) {
                this.collisionClass = className;
            }
        },
        
        //Trigger some events on player collision. Paremeter is collided object.
        trigger: function(object) {
            if($(object).hasClass('trigger') && !$(object).hasClass('hasTriggered')) { //Check to see if the object has the class 'trigger' and not has triggered
                $(object).addClass('hasTriggered');
                
                if($(object).hasClass('NPC')) { //Triggers the triggerIf action from XMAP if the given object is an NPC
                    var id = $(object).attr("id");
                    character.triggerIfAction(id);
                }
                
                if($(object).hasClass('damage')) { //Trigger damage if the object has a damage class
                    player.removeHealth();
                } 
                
                if($(object).hasClass('health')) { //Trigger health if the object has a health class
                    player.addHealth();
                } 
                
                if($(object).hasClass('jewels')) { //Trigger jewels if the object has a jewel class
                    player.addJewels();
                } 
                
                if($(object).hasClass('mapConnection')) { //Loads the next map and moves the player
                    var map = $(object).attr('id');
                    var moveX = parseInt($('#nextSpawn' + map).css('left'));
                    var moveY = parseInt($('#nextSpawn' + map).css('top'));
                    player.move(moveX, moveY);
                   server.loadMap(map);
                }
                
                if($(object).hasClass('quest')) { //Shows the quiest if the object has a quest
                    var id = $(object).attr("id");
                    character.showQuest(id, false);
                } 
            }     
        },
        
        checkSound: function(object) {
            var id = $(object).attr("id");
            
            if($(object).hasClass('hasSound')) {
                sound.playSoundByObjectID(id);
            }
            
            if($(object).hasClass('hasSurfaceSound') && !$(object).hasClass('surfaceSoundSet')) {
                sound.setSurfaceSound(id);
                $(object).addClass('surfaceSoundSet');
            }
        }
        
    }